'''
Created on 28 sept. 2010
@author: Marc CARRE
@contact: carre.marc@gmail.com
@license: GNU GPLv3
@version: 1.0
'''

import random

class PointGenerator:

    def __init__(self, params, limits):
        self.checkInput(params, limits)
        self.params = params
        self.limits = limits
        
        
    def checkInput(self, params, limits):
        self.checkInputType(params, limits)
        self.checkInputRelevance(params, limits)
        
        
    def checkInputType(self, params, limits):
        if type(params) != list:
            raise ValueError("Parameters must be an array of string, e.g. : ['x',""y""].")
        
        for param in params:
            if not type(param) is str:
                raise ValueError("Parameters' values must be string objects, e.g. : 'x' or ""y"".")
            
        if type(limits) != list:
            raise ValueError("Limits must be an array of array of numerics, e.g. : [ [-2, 2], [0.1, 4] ].")
        
        for limit in limits:
            if type(limit) != list:
                raise ValueError("Each limit object must be a pair of numerics [min, max], e.g. : [-2, 2], or [0.1, 4].")
            for elem in limit:
                if (type(elem) != int) and (type(elem) != float):
                    raise ValueError("Each limit object must be a pair of numerics [min, max], e.g. : [-2, 2], or [0.1, 4].")
        
        
    def checkInputRelevance(self, params, limits):
        if len(limits) != len(params):
            raise ValueError("Incorrect number of limits: you should have one limit for each parameter, e.g. : ['x','y'] -> [ [-2, 2], [0.1, 4] ].")
        
        for limit in limits:
            if len(limit) != 2:
                raise ValueError("Incorrect limit: you should have two numerics, one minimum, one maximum, for each limit object, e.g. : [-2, 2].")
            if limit[0] >= limit[1]:
                raise ValueError("Incorrect limit: the minimum should be strictly less than the maximum, e.g. : [-2, 2].")
            
    def generateFloat(self, param):
        if param not in self.params:
            raise ValueError("Incorrect parameter, must be in the given list of parameters.")
        idx = self.params.index(param)
        limit = self.limits[idx] # Get the min/max for the given parameter.
        return random.uniform(limit[0], limit[1])   
    
    def generateInt(self, param):
        if param not in self.params:
            raise ValueError("Incorrect parameter, must be in the given list of parameters.")
        idx = self.params.index(param)
        limit = self.limits[idx] # Get the min/max for the given parameter.
        return random.randint(limit[0], limit[1])
    